package com.qlm.tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.upload.UploadFile;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qlm.view.ResultMessage;

/**
 * 七牛云服务
 * 
 * @author 张攀
 *
 */
public class QiniuUtil {

	private static final String ACCESS_KEY = "SqGH7z4yWmdKajW3FCJbuh2kkte-OHI0HeePVC6l";
	private static final String SECRET_KEY = "6V7H8ezuqpCWLSP7jqoahlbUf4PWryWeQn4RYT0e";
	private static final String BUCKET_NAME = "shop";
	private static Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
	private static BucketManager bucketManager = new BucketManager(auth);
	private static final Logger logger = LoggerFactory.getLogger(QiniuUtil.class);
	
	public static final String IMAGEURL = "http://g.grfruitc.top/";     //所有空间图片的访问路径
	
	/** 
	 * 上传图片到七牛云存储 
	 * @param reader 
	 * @param fileName 
	 * @return String
	 */
	public static String upload(String pathFile, String key) {
		String str = null;
		try {
			UploadManager uploader = new UploadManager();
			String mime = "image/JPG";
			String token = getUpToken();
			Response res = uploader.put(pathFile, key, token, null, mime, false);
			str = res.bodyString();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return str;
	}
	
	/**
	 * 上传证书文件
	 * */
	public static String uploadP12(String pathFile,String key){
		String str = null;
		try {
			UploadManager uploader = new UploadManager();
			String mime = "p12";
			String token = getUpToken();
			Response res = uploader.put(pathFile, key, token, null, mime, false);
			str = res.bodyString();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return str;
	}
	
	
	/**      
	 * 删除七牛云存储上的图片      
	 *  @param key      
	 */     
	public static void delete( String key) {        
		try {
			bucketManager.delete(BUCKET_NAME, key);
		} catch (QiniuException e) {
			logger.error(e.getMessage());
		}
	}
	
	/**
	 * 下载
	 * @param key
	 */
	public static void download(String key){
		String urlSigned = auth.privateDownloadUrl("http://7xk1qf.com1.z0.glb.clouddn.com/"+key+".jpg");
		System.out.println(urlSigned);
	}
	// 简单上传，使用默认策略
	private static String getUpToken(){
	    return auth.uploadToken(BUCKET_NAME);
	}
	
	/**
	 * 上传图片到七牛云，并返回resultMessage对象 
	 * @param imgFile
	 * @return
	 */
	public static ResultMessage uploadImgToQiniu(UploadFile imgFile){
		ResultMessage resultMessage = new ResultMessage();
		if (imgFile != null) {
			String fileName = Common.getStringFullDate() + imgFile.getFileName(); // 文件名称
			try {
				String success = QiniuUtil.upload(imgFile.getFile().getPath(),
						fileName); // 上传到七牛云服务
				if (success != null) {
					imgFile.getFile().delete(); // 如果上传成功，就删除本地服务的图片
					resultMessage.setState(0);
					resultMessage.setMessage("文件上传成功");
					resultMessage.setData(fileName);
				}
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				resultMessage.setState(1);
				resultMessage.setMessage("文件上传失败");
			}
		}
		return resultMessage;		
	}
}
